package com.example.mobiledestroy.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class MD5 {
	
	private String msg;
	private byte[] msg2;


	public MD5(String msg){
		this.msg = msg;
	}
	
	public MD5(byte[] msg2){
		this.msg2 = msg2;
	}
	
	public String getMD5String() {
		byte[] a = encrypt(msg);
		String string = new String(a);
		Log.i("md5", string);
		return string;
		
	}
	
	public byte[] getMD5Byte() {
		byte[] a = encrypt(msg);
		return a;
	}

	private byte[] encrypt(String msg){
		//根据MD5算法生成MessageDigest对象
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] srcBytes = msg.getBytes();
			Log.i("byte",srcBytes.toString());
			//使用srcBytes更新摘要
			md5.update(srcBytes);
			//完成哈希计算,得到result
			byte[] resultBytes= md5.digest();
			return resultBytes;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private byte[] encrypt(byte[] msg2){
		//根据MD5算法生成MessageDigest对象
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			Log.i("byte",msg2.toString());
			//使用srcBytes更新摘要
			md5.update(msg2);
			//完成哈希计算,得到result
			byte[] resultBytes= md5.digest();
			return resultBytes;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
